import base from './base'
import wepy from 'wepy'
import auth from './auth'
/**
 * 保存用户学习路径
*/
export default class saveProgress extends base {
  static async save(param = {}, back = false) {
    let timestamp = (new Date()).getTime()
    let openId = wepy.getStorageSync('open_id') || ''
    // 第一次进入小程序的时候初始化上一次进入的时间
    if (param.type === 1) {
      wepy.setStorageSync('lastTime', timestamp)
    }
    let lastTime = wepy.getStorageSync('lastTime') || timestamp
    // 在页面停留时间
    let deltaTime = timestamp - lastTime
    wepy.setStorageSync('lastTime', timestamp)

    let pages = getCurrentPages()
    // ！！！首页进来pageFrom 是index 需要单独在传参的时候传入pageFrom='',覆盖这里的参数
    let pageFrom = ''
    let pageTo = ''
    if (back) {
      pageTo = pages[pages.length - 1].route.split('/')[1]
    } else {
      pageFrom = pages[pages.length - 1].route.split('/')[1]
    }
    console.log(pageFrom)
    let studyOption = wepy.getStorageSync('studyOption') || {}
    let knowledgeNodeId = wepy.getStorageSync('studyingId') || ''
    let obj = {
      openId,
      timestamp,
      pageFrom,
      pageTo,
      colligatePackageId: studyOption.colligatePackageId || '',
      subjectId: studyOption.subjectId || '',
      studyPackageId: studyOption.stuPackageId | '',
      knowledgeNodeId,
      type: '',
      duration: Math.round(deltaTime / 1000),
      durationTime: deltaTime
    }
    Object.assign(obj, param)
    let url = this.baseUrl + `/clientUser/user.behavior`
    this.post(url, obj)
    // console.log(openId,time)
  }
  // bi-5 统计注册信息
  static async registerRecord(param = {}) {
    const url = this.urlMap.caoxin + `/stuRegistRecord.post`
    // const {flag} = await auth.isNewUser() // 是否是新用户
    let flag = auth.getConfig('isNewUser')
    if (!flag) return // 老用户
    // 由于isNewUser的值从后台只获取一次，使用后立即改变。与后台同步
    auth.setConfig('isNewUser', false)
    // 登录页面次数区分是否是尚德学员
    // let {userType} = await wepy.$instance.globalData.auth
    // this.videoLog({Type: 1, clickType: Number(userType) === 0 ? 17 : 16})
    let obj = {
      openId: auth.getConfig('open_id'),
      scene: wepy.$instance.globalData.scene // 场景值
    }
    let scene = wepy.$instance.globalData.scene
    if ((scene === 1011 || scene === 1012 || scene === 1013 || scene === 1047 || scene === 1048 || scene === 1049) && !param.invitedOpenId) {
      // 二维码进来或者小程序码
      Object.assign(obj, {
        type: 2
      })
    } else if (param.invitedOpenId) {
      // 分享进来 5 用户中心 4 视频分享 1 资料领取
      let pageMap = {
        'mine': 5,
        'lectureItem': 4,
        'dataDownload': 1
      }
      Object.assign(obj, {
        type: pageMap[param.fromPage]
      })
    } else {
      // 用户直接进来
      Object.assign(obj, {
        type: 3
      })
    }

    // let obj = {
    //   openId: auth.getConfig('open_id'),
    //   invitedOpenId: '', // 受邀请的openid
    //   scene: wepy.$instance.globalData.scene, //场景值
    //   theme: 'newYearMaterial', // 样式
    //   qrcodeId: '2', // 渠道id
    //   type // type: 1 分享 2 二维码 3 用户直接进去
    // }

    Object.assign(obj, param)
    return this.post(url, obj)
  }
  // bi-4 分享行为
  static stuStudyDataShare (params) {
    let url = this.baseUrl + `/stuStudyDataShare.post`
    return this.post(url, params)
  }
  static dataLog(params) {
    let url = this.baseUrl + '/v2/dataLog/dataLog.put'
    Object.assign(params, {openId: auth.getConfig('open_id')})
    return this.post(url, params)
  }
  static dataAuthLog(params) {
    let url = this.baseUrl + '/v2/dataLog/authData.put'
    Object.assign(params, {openId: auth.getConfig('open_id')})
    return this.post(url, params)
  }
    // v2.0埋点
  static videoLog(params) {
    let url = this.baseUrl + '/v2/buriedPoint/saveBurjedProint.post'
    return this.post(url, Object.assign(params, {openId: auth.getConfig('open_id')}))
  }
  // v2.1埋点 全部埋点集合
  static log(params) {
    let url = this.urlMap.caoxin + '/v2/buriedPoint/saveBurjedProint.post'
    let openId = wepy.getStorageSync('open_id')
    let initParams = {
      'Type': 1,
      openId,
      'dataType': '',
      'welcomePage': '',
      'loginPage': '',
      'intentionPage': '',
      'homePage': '',
      'lectureList': '',
      'lectureDetails': '',
      'myPage': '',
      'downloadList': '',
      'downloadDetails': '',
      'aiPage': '',
      'evaluationPage': ''
    }
    return this.post(url, Object.assign(initParams, params))
  }
}
